set seed 12345
local reps = 100 // number of bootstrap replications for the decomposition table

**************** Worker performance *************
insheet using "Data/lab2_stage1.csv", clear

rename score scorea
forval y0=2/5 {
	gen temp=(scorea==`y0')
	summ temp if gender == 0
	local weight_`y0' = r(mean)
	summ temp if gender == 1
	local weight_`y0' = (`weight_`y0''+r(mean))/2
	drop temp
}

reg hire gender, r

**************** Manager Hiring *****************
insheet using "Data/lab2_stage2.csv", clear 

reg hire gender, r //high qualification systemic

gen weight=1
rename w1_scorea scorea
forval y0=2/5 {
	replace weight = `weight_`y0'' if scorea==`y0'
}

gen gender_g=1
replace gender_g=0 if sex_g=="Female" 
gen college=1
replace college=0 if education_g=="High School or equivalent"
replace college=0 if education_g=="Some College"
gen employed=1
replace employed=0 if employment!="Employed (full/part time)"
gen white=0
replace white=1 if race=="White"
gen rHire = 0
replace rHire = 1 if recruiter_hiring=="Hired"
rename gender sex

************** Figure A.1 -- Signal Densities and Hiring Rates ***************
preserve
clear
tempfile file1 file2
graph set window fontface "Arial"
import delimited "Data/lab2_stage2.csv"
rename hire hireH
gen hireR = .
replace hireR = 1 if recruiter_hiring == "Hired"
replace hireR = 0 if recruiter_hiring == "Not hired"
rename w1_scorea scoreA
rename w1_scoreb scoreB
keep scoreA scoreB gender hireH hireR
gen sig = -1
replace sig = 0 if scoreA < 4
replace sig = 2 if scoreA > 3
save file1, replace
collapse (mean) hireR (semean) sehireR = hireR, by(gender sig)
save file2, replace
use file1, clear
replace sig = 1 if hireR == 0
collapse (mean) hireH (semean) sehireH = hireH (count) n = scoreB, by(gender sig)
merge 1:1 gender sig using file2
drop _merge
replace sig = sig + 0.4 if sig == 0
replace sig = sig - 0.4 if sig == 2
gen graphL = sig - 0.05
gen graphR = sig + 0.05
egen N = sum(n), by(gender)
gen density = n/N
replace density = hireR if sig != 1

twoway scatter hireH sig if gender==0, color(maroon) yaxis(1) ytitle("Average Hiring Rate", axis(1)) ///
|| scatter hireH sig if gender==1, color(blue) yaxis(1) ///
|| lfit hireH sig if gender==0, color(maroon) yaxis(1)  ///
|| lfit hireH sig if gender==1, color(blue) yaxis(1) ///
|| bar density graphR if gender==0, barw(0.1) color(maroon%20) yaxis(2) yscale(range(0) axis(2)) ytitle("Signal Density", axis(2)) ylabel(#10, axis(2)) ///
|| bar density graphL if gender==1, barw(0.1) color(blue%20) yaxis(2) ///
|| , legend(order(1 "Female Hiring Rate" 2 "Male Hiring Rate" 5 "Female Signal Density" 6 "Male Signal Density") position(6)) xlabel(0.4 "Low" 1 "None" 1.6 "High") xtitle("Hiring Manager Signal") graphregion(color(white))

gr_edit legend.Edit , style(cols(2)) style(rows(0)) keepstyles 
gr_edit .style.editstyle declared_ysize(5.5) editcopy
gr_edit .legend.Edit, style(labelstyle(drawbox(yes))) style(labelstyle(linestyle(pattern(solid))))
gr_edit .legend.Edit, style(labelstyle(drawbox(no)))
gr_edit .legend.style.editstyle boxstyle(linestyle(color(black))) editcopy
gr_edit .xaxis1.style.editstyle draw_major_grid(no) editcopy
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(pattern(solid)))) editcopy

graph export "Output/lab2fig.png", replace
restore

****** Table A.1 -- Total, Direct, and Systemic Discrimination in Manager Wages ******

tempfile base
save `base'
forval bs=0/`reps' {
disp "Bootstrap replication # `bs'"
qui {
use `base', clear
if `bs' > 0 {
    gen bs_weight=-ln(uniform())
	replace weight=weight*bs_weight
}

matrix decomp=J(7,7,0)
preserve
replace scorea = -1 if rHire == 0 
collapse (mean) hire [aw=weight], by(scorea sex)
qui reshape wide hire, i(scorea) j(sex)
gen direct = hire1 - hire0
mkmat direct, matrix(direct)
restore

local col = 1
forval y0 = 2/5 {
	qui reg hire sex if scorea==`y0' [aw=weight]
	matrix decomp[1,`col'] = _b[sex]
	qui summ sex if scorea==`y0' [aw=weight]
	local totM=r(N)*r(mean)
	local totF=r(N)*(1-r(mean))
	qui summ sex if rHire == 1 & scorea==`y0' [aw=weight]
	local Mweights = r(N)*r(mean)/`totM'
	if `Mweights' ==. {
			local Mweights = 0 
		}
	local Fweights = r(N)*(1-r(mean))/`totF'
	if `Fweights' ==. {
			local Fweights = 0 
		}
	matrix decomp[2,`col'] = `Mweights'*direct[`y0',1]+(1-`Mweights')*direct[1,1]
	matrix decomp[4,`col'] = `Fweights'*direct[`y0',1]+(1-`Fweights')*direct[1,1]
	matrix decomp[3,`col'] = decomp[1,`col'] - decomp[2,`col']
	matrix decomp[5,`col'] = decomp[1,`col'] - decomp[4,`col']
	matrix decomp[6,`col'] = (decomp[2,`col']+decomp[4,`col'])/2
	matrix decomp[7,`col'] = (decomp[3,`col']+decomp[5,`col'])/2
	local col = `col' + 1
	disp "scorea `y0' completed"
}

forval row=1/7 {
	matrix decomp[`row',5]=(decomp[`row',1]*`weight_2'+decomp[`row',2]*`weight_3')/(`weight_2'+`weight_3')
	matrix decomp[`row',6]=(decomp[`row',3]*`weight_4'+decomp[`row',4]*`weight_5')/(`weight_4'+`weight_5')
	matrix decomp[`row',7]=decomp[`row',6]-decomp[`row',5]
}

if `bs' == 0 {
    matrix decomp_est = decomp
}
if `bs' > 0 {
    preserve
	clear
	svmat decomp
	gen rep=`bs'
	gen j=_n
	reshape wide decomp1 decomp2 decomp3 decomp4 decomp5 decomp6 decomp7, i(rep) j(j)
	tempfile rep_`bs'
	save `rep_`bs''
	restore
}
}
}
clear
use `rep_1'
forval bs = 2/`reps' {
    append using `rep_`bs''
}
matrix decomp_se=J(7,7,.)
forval col=1/7 {
    forval row=1/7 {
	    qui summ decomp`col'`row'
		matrix decomp_se[`row',`col']=r(sd)
	}
}
matrix list decomp_est
matrix list decomp_se

*see r1, r6, r7 for Total, Average Direct, and Systemic, respectively
*see c6, c5, c7 for High Qualification, Low Qualification, and Difference, respectively

use `base', clear
